package cn.edu.web;

import cn.edu.pojo.*;

import cn.edu.service.TeacherService;

import cn.edu.service.impl.TeacherServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @author Davey chan
 */
@WebServlet("/Teacher/*")
public class TeacherServlet extends BaseServlet {
    private TeacherService tService = new TeacherServiceImpl();

    //分页查询
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


//接受当前页码，和每页展示条数
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize= Integer.parseInt(_pageSize);

        PageBean<All> pageBean = tService.selectByPage(currentPage, pageSize);


//将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(pageBean);
//        响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    //分页查询
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //        处理中文乱码
        request.setCharacterEncoding("utf-8");
//接受当前页码，和每页展示条数
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        String _academyName = request.getParameter("academyName");
        byte[]  bytes = _academyName.getBytes("iso-8859-1");
        String  academyName = new String(bytes,"utf-8");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize= Integer.parseInt(_pageSize);
//获取查询的封装对象

        BufferedReader br=request.getReader();
        String s = br.readLine();
        //        将JSON字符串转成JAVA对象
        All t = JSON.parseObject(s, All.class);
        PageBean<All> pageBean = tService.selectByPageAndcondition(currentPage, pageSize,academyName,t);


//将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(pageBean);
//        响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
//    删除教师

public void delteacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //接收页面返回的教师ID
    String _id = request.getParameter("id");
    int id = Integer.parseInt(_id);

   //        调用Service的同意
    tService.delteacher(id);
//        返回成功的标识
    response.getWriter().write("ok");
}
    public void UpdateTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回修改后的数据值
        //        处理中文乱码
        request.setCharacterEncoding("utf-8");
        BufferedReader br=request.getReader();
        String s = br.readLine();
        //        将JSON字符串转成JAVA对象
        All t = JSON.parseObject(s, All.class);
        //        调用Service的同意
        System.out.println(t.toString());
        tService.Updateteacher(t);
//        返回成功的标识
        response.getWriter().write("ok");
    }
    public void getThesisList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<thesis> thesislist = tService.getThesis(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(thesislist);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }

    public void getResearch_project(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<research_project> research_project = tService.getResearch_project(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(research_project);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }

    public void getAnnual_examination(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<annual_examination> annual_examination = tService.getAnnual_examination(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(annual_examination);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }
    public void getAward(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<award> award = tService.getAward(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(award);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }
    public void getHonor_title(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<honor_title> honor_title = tService.getHonor_title(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(honor_title);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }
    public void getOpus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<opus> opus = tService.getOpus(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(opus);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }
    public void getTeach_work(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<teach_work> teach_work = tService.getTeach_work(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(teach_work);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }
    public void getTrain(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<train> train = tService.getTrain(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(train);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }
    public void getPatent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<patent> patent = tService.getPatent(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(patent);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }
    public void getWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面返回的教师ID
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        //        调用Service的同意
        List<work> work = tService.getWork(id);
        //将集合转换成JSON数据，也叫序列化
        String jsonString = JSON.toJSONString(work);
//        响应数据
        response.setContentType("text/json;charset=utf-8");

//        返回成功的标识
        response.getWriter().write(jsonString);
    }
}
